//
//  SevenDetailViewController.swift
//  swiftTS
//
//  Created by 航电 on 2020/4/7.
//  Copyright © 2020 航电. All rights reserved.
//

import UIKit

class SevenDetailViewController: UIViewController {
    
    fileprivate let cellId = "seven_detail_collectionCell_ID";
    fileprivate let suppleKind = "seven_collection_king";
    fileprivate var _mainCollectionView:UICollectionView!
    fileprivate var _lastContentOffSetX:CGFloat = 0;
    fileprivate lazy var _mutArr:NSMutableArray = {
        var mutArr = NSMutableArray();
        mutArr.add(["pic":"","title":"","subTitle":""]);
        mutArr.add(["pic":"","title":"","subTitle":""]);
        mutArr.add(["pic":"","title":"","subTitle":""]);
        mutArr.add(["pic":"","title":"","subTitle":""]);
        mutArr.add(["pic":"","title":"","subTitle":""]);
        return mutArr;
    }();
    
    override func viewDidLoad() {
        super.viewDidLoad()
        self.title = "transition";
        // Do any additional setup after loading the view.
        self.view.backgroundColor = UIColor(red: 239.0/255.0, green: 239.0/255.0, blue: 244.0/255.0, alpha: 1.0);
        
        self.onCreateUIForView();
    }
    

    //TODO:UI
    fileprivate func onCreateUIForView() {
        let layout = UICollectionViewFlowLayout();
        layout.scrollDirection = .horizontal;
        
        _mainCollectionView = UICollectionView(frame: CGRect(x: 0, y: 0, width: screenMainSize.width, height: screenMainSize.height), collectionViewLayout: layout);
        _mainCollectionView.backgroundColor = .clear;
        _mainCollectionView.delegate = self;
        _mainCollectionView.dataSource = self;
        _mainCollectionView.register(SevenDetailCell.self, forCellWithReuseIdentifier: cellId);
        _mainCollectionView.register(UICollectionReusableView.self, forSupplementaryViewOfKind: UICollectionView.elementKindSectionHeader, withReuseIdentifier: suppleKind);
        _mainCollectionView.register(UICollectionReusableView.self, forSupplementaryViewOfKind: UICollectionView.elementKindSectionFooter, withReuseIdentifier: suppleKind);
        _mainCollectionView.isPagingEnabled = true;
        self.view .addSubview(_mainCollectionView);
    }

    //TODO:Actions
    fileprivate func onShowAnimation() -> CATransition {
        let transition = CATransition();
        /* CATransitionType
         fade 淡入
         moveIn 从指定方向慢慢进入
         push 从指定方向推进
         reveal 揭开
         */
        transition.type = .fade;
        
        /*CATransitionSubtype 切换方向
         fromRight
         fromLeft
         fromTop
         fromBottom
         */
        transition.subtype = .fromBottom;
        
        transition.duration = 5;
        
        return transition;
    }
}

extension SevenDetailViewController:UICollectionViewDelegate {
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        let cell:SevenDetailCell = collectionView.cellForItem(at: indexPath) as! SevenDetailCell;
        if cell.isKind(of: SevenDetailCell.self) {
            cell.onChangeContentView(isNext: !cell._isShowNextView);
        }
    }
}

extension SevenDetailViewController:UICollectionViewDelegateFlowLayout {
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
        return CGSize(width: collectionView.frame.width-100, height: 460);
    }

    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets {
        let topMarg = (collectionView.frame.height-460.0)/2.0;
        return UIEdgeInsets(top: topMarg, left: 0, bottom: topMarg, right: 0);
    }

    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat {
        return 20;
    }

    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAt section: Int) -> CGFloat {
        return 0;
    }

    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForHeaderInSection section: Int) -> CGSize {
        return CGSize(width: 50, height: 0);
    }

    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForFooterInSection section: Int) -> CGSize {
        return CGSize(width: 50, height: 0);
    }
}

extension SevenDetailViewController:UICollectionViewDataSource {
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return _mutArr.count;
    }

    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: cellId, for: indexPath);
    
        return cell;
    }
    
    func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {
        let view = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: suppleKind, for: indexPath);
        view.backgroundColor = .clear;
        return view;
    }
}

extension SevenDetailViewController:UIScrollViewDelegate {
    func scrollViewWillBeginDecelerating(_ scrollView: UIScrollView) {
        let cellWidth = _mainCollectionView.frame.width-100;
        let currentOffSetX = scrollView.contentOffset.x;
        var currentIndex = Int(currentOffSetX/cellWidth);
        
        let y = scrollView.contentOffset.y;
        var currentLeftMarg:CGFloat = 0.0;
        if currentOffSetX > _lastContentOffSetX {
            //往后显示下一张
            if currentIndex >= _mutArr.count-1 {
                currentIndex = _mutArr.count-2;
            }
            currentLeftMarg = (cellWidth+20)*CGFloat(currentIndex+1);
        } else {
            //往前显示前一张
            currentLeftMarg = (cellWidth+20)*CGFloat(currentIndex);
        }
        
        scrollView.setContentOffset(CGPoint(x: CGFloat(currentLeftMarg), y: y), animated: true);
        _lastContentOffSetX = currentOffSetX;
    }

}
